package com.cjk.console.interceptor;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.cjk.console.SessionApi;
import com.cjk.console.model.LoginInfo;
import com.cjk.foundation.commons.BusinessException;

public class LoginInterceptor extends HandlerInterceptorAdapter {
	private List<String> unInterceptUrls = new ArrayList<>();
	public LoginInterceptor() {
		unInterceptUrls.add("/portal/login.do");
		unInterceptUrls.add("/portal/logout.do");
		unInterceptUrls.add("/error/resolve_404.do");
	}
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		final String url = request.getServletPath();
		
		if (url.startsWith("/app")) {
			return true;
		}
		
		if (url.startsWith("/download")) {
			return true;
		}
		
		if (url.startsWith("/file")) {
			return true;
		}
		
//		if (url.startsWith("/mqtt")) {
//			return true;
//		}
		
		if (!unInterceptUrls.contains(url)) {
			LoginInfo loginInfo = SessionApi.getLoginUserInfo(request.getSession());
			if (loginInfo == null) {
				throw new BusinessException("TIME_OUT");
			}
		}
		return true;
	}

}
